package org.example.now.controller;

import org.example.now.entity.UserInfoDto;
import org.example.now.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;


    @GetMapping(value = "findAll")
    public void findAll(){
        PageRequest pageRequest = PageRequest.of(2,3);
        Page<UserInfoDto> page = userService.getUserByPage(pageRequest);
        System.out.println("总页数："+page.getTotalPages());
        System.out.println("总记录数："+page.getTotalElements());
        System.out.println("查询结果："+page.getContent());
        // 从0开始记，所以加上1
        System.out.println("当前页数："+page.getNumber()+1);
        System.out.println("当前记录数："+page.getNumberOfElements());
        System.out.println("每页记录数："+page.getSize());
    }

    @GetMapping(value = "search")
    public void search(){
        List<UserInfoDto> bs1 = userService.getUserByIdAndName("鲁迅",7);
        List<UserInfoDto> bs2 = userService.getUserByUserCodeStartingWith("吴");
        List<UserInfoDto> bs3 = userService.getUserByIdAndName("西",8);
        List<UserInfoDto> bs4 = userService.getUserByCreateThen(3);

        UserInfoDto b = userService.getMaxIdBook();
        System.out.println("bs1:"+bs1);
        System.out.println("bs2:"+bs2);
        System.out.println("bs3:"+bs3);
        System.out.println("bs4:"+bs4);
        System.out.println("b:"+b);
    }

    @GetMapping(value = "/save")
    public void save(){
        UserInfoDto user = new UserInfoDto();
        user.setUserName("怀致远");
        user.setUserCode("181818");
        user.setCreateDate("2024-02-18");
        userService.addUser(user);
    }

    @GetMapping(value = "/update")
    public void update(){
        userService.updateUserName("131313","宫鹏鲸");
        userService.updateUserName2("141414","寿志诚");
    }



}
